package com.gm.baseplatform.core.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 
 * http connection 
 * @see java.net.HttpURLConnection
 * 
 * @author wangguomei
 * @mail e.wanggm@gmail.com
 * 
 */
public abstract class HttpConnectionUtil {

	
	public static String doHttpConnection(
			String requestURL,
			String requestMethod,
			int connectTimeout,
			int readTimeout,
			String writerData,
			String readerEncoding) throws Exception {
		
		HttpURLConnection httpURLConnection = null;
		BufferedWriter out = null;
		BufferedReader in = null;
		
		StringBuilder responseData = new StringBuilder();
		
		try {
	
			httpURLConnection = (HttpURLConnection) new URL(requestURL).openConnection();
	        httpURLConnection.setRequestProperty("Connection", "close");
	        httpURLConnection.setRequestProperty("content-type", "text/html");
	        httpURLConnection.setRequestMethod(requestMethod != null ? requestMethod : "POST");
	        httpURLConnection.setConnectTimeout(connectTimeout != 0 ? connectTimeout : 50000);
	        httpURLConnection.setReadTimeout(readTimeout != 0 ? readTimeout : 50000);
	        httpURLConnection.setDoOutput(true);
	        httpURLConnection.setDoInput(true);
	        httpURLConnection.connect();
	        if (writerData != null) {
	        	out = new BufferedWriter(new OutputStreamWriter(
	  			       httpURLConnection.getOutputStream()));
	  		    out.write(writerData);
	  	        if(out != null){
	  		       out.flush();
	  	        }
	        }
	        in = new BufferedReader(new InputStreamReader(
					 (InputStream) httpURLConnection.getInputStream(),
					 readerEncoding != null ? readerEncoding : "UTF-8"));
		   if (in != null) {
			   String temp = null;
			   while ((temp = in.readLine()) != null) {
					responseData.append(temp);
			   }
		   }
		} 
		catch (Exception e) {
		   throw e;
		}
		finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(httpURLConnection != null){
				try {
				   httpURLConnection.disconnect();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		
		return responseData.toString();
		
	}
	
	
	public static String doHttpConnection(String requestURL) throws Exception {
		return doHttpConnection(requestURL,null,0,0,null,null);
	}
	
	
	public static String doHttpConnection(String requestURL,String writerData) throws Exception {
		return doHttpConnection(requestURL,null,0,0,writerData,null);
	}
	
	
	public static String doHttpConnection(String requestURL,String writerData,String readerEncoding) throws Exception {
		return doHttpConnection(requestURL,null,0,0,writerData,readerEncoding);
	}
	
	
}
